package com.chujian.file.service.impl;

import com.chujian.file.service.IOssService;
import com.chujian.framework.starter.aliyun.oss.AliOssClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Component
public class AliOssServiceImpl implements IOssService {

    @Autowired
    private AliOssClient ossClient;

    @Value("${chujian.aliyun.oss.default-bucket}")
    private String bucketName;

    @Override
    public String fileUpload(String bucket, String path, MultipartFile file) throws IOException {
        if (StringUtils.isEmpty(bucket)) {
            bucket = bucketName;
        }
        return ossClient.putObject(bucket, path, file.getInputStream());
    }

    @Override
    public String fileUpload(String path, MultipartFile file) throws IOException {
        return fileUpload(path, file.getInputStream());
    }

    @Override
    public String fileUpload(String path, InputStream in) {
        return ossClient.putObject(bucketName, path, in);
    }

    @Override
    public InputStream getObject(String bucketName, String objectName) {
        return ossClient.getObject(bucketName, objectName);
    }
}
